/**
 * 
 */
package com.kunbo.cn.service.impl.core.inventory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import com.kunbo.cn.dao.core.inventory.CarrierUserDao;
import com.kunbo.cn.entity.core.inventory.CarrierUser;
import com.kunbo.cn.entity.system.user.User;
import com.kunbo.cn.service.core.inventory.CarrierUserService;
import com.kunbo.cn.util.date.SDate;
import com.kunbo.cn.util.encrypt.MD5Utils;

/**
 *
 * @author bjl  
 * @date 2019年3月14日 
 */
@Service
public class CarrierUserServiceImpl implements CarrierUserService {
	@Autowired
    private  JdbcTemplate jdbc;
	@Autowired
	private CarrierUserDao CauDao;
	@Override
	public void save(CarrierUser cuser) {
		cuser.setCreate_time(SDate.getSystemDateToString());
		cuser.setPassword(MD5Utils.md5(cuser.getPassword()));
		CauDao.save(cuser);
	}

	@Override
	public void update(CarrierUser cuser) {
		CauDao.save(cuser);
		
	}

	@Override
	public void delete(String id) {
		CauDao.delete(id);
		
	}

	@Override
	public CarrierUser findById(String id){
		return CauDao.findOne(id);
	}
	@Override
	public Page<CarrierUser> findCarrierUser(String carrier_code, String carrier, String login_account, Integer pageNo,
			Integer pageSize) {
		Pageable pageable = new PageRequest(pageNo-1, pageSize);
        Specification<CarrierUser> confusion=new Specification<CarrierUser>() {
            @Override
            public Predicate toPredicate(Root<CarrierUser> root, CriteriaQuery<?> query,
                    CriteriaBuilder criteriaBuilder) {
            	List<Predicate> predicates = new ArrayList<>();
            	if(!"".equals(carrier_code)) {
            		predicates.add(criteriaBuilder.like(root.get("carrier_code"), "%"+carrier_code+"%"));
            	}
            	if(!"".equals(carrier)) {
            		predicates.add(criteriaBuilder.like(root.get("carrier"), "%"+carrier+"%"));
            	}
            	if(!"".equals(login_account)) {
            		predicates.add(criteriaBuilder.like(root.get("login_account"), "%"+login_account+"%"));
            	}
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        return CauDao.findAll(confusion, pageable);
	}

	@Override
	public List<Map<String, Object>> CarrierUserCount() {
		return null;
	}

	@Override
	public List<Map<String, Object>> showMaterTypeMes(String code, String materialType, Integer page,
			Integer pageSize) {
		return null;
	}

}
